#include <iostream>
#include "../common/httplib.h"
#include "./oj_control.hpp"

using namespace httplib;

int main()
{
    // 用户请求的服务理由功能
    Server svr;
    
    ns_control::Control con;

    // 获取所有的题目列表
    svr.Get("/all_questions",[&](const Request &req,Response &resp)
        {
            std::string html;
            con.AllQuestions(&html);
            resp.set_content(html,"text/html; charset=utf-8");
        });

    // 用户根据题目编号来获取题目内容
    svr.Get(R"(/question/(\d+))",[&](const Request &req,Response &resp)
        {
            std::string number=req.matches[1];
            std::string html;
            
            con.OneQuestion(number,&html);

            resp.set_content(html,"text/html; charset=utf-8");
        });

    // 用户提交代码,使用我们的判题功能
    svr.Get(R"(/judge/(\d+))",[&](const Request &req,Response &resp)
        {
            std::string number=req.matches[1];
            std::string out_json;

            con.Judge(number,req.body,&out_json);
            
            resp.set_content(out_json,"application/json;charset=utf-8");
        });

    svr.set_base_dir("./wwwroot");

    svr.listen("0.0.0.0",8080);

    return 0;
}

/*
// 获取所有的题目列表
    svr.Get("/all_questions",[](const Request &req,Response &resp)
        {
            resp.set_content("这是所有题目的列表","text/plain; charset=utf-8");
        });

    // 用户根据题目编号来获取题目内容
    svr.Get(R"(/question/(\d+))",[](const Request &req,Response &resp)
        {
            std::string number=req.matches[1];
            resp.set_content("这是指定的一道题: " + number,"text/plain; charset=utf-8");
        });

    // 用户提交代码,使用我们的判题功能
    svr.Get(R"(/judge/(\d+))",[](const Request &req,Response &resp)
        {
            std::string number=req.matches[1];
            resp.set_content("对指定题目判题: " + number,"text/plain; charset=utf-8");
        });
*/